home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / atob11.zip / ATOB.ASM next >
Assembly Source File  |  1990-09-07  |  27KB  |  789 lines

  1. ;*************************************************************************
  2. ;
  3. ; atob.asm:  Fast assembly-language version of atob.c
  4. ;            Version 1.1
  5. ;
  6. ; atob converts files from ascii to binary, undoing the encoding
  7. ; of btoa.
  8. ;
  9. ; USAGE:
  10. ;               atob output.fil <input.fil
  11. ;
  12. ; DESCRIPTION:
  13. ;
  14. ; atob reads its stdin and sends its output to the designated output file.
  15. ; If it already exists, the output file is overwritten *without*warning*.
  16. ;
  17. ; The encoding is performed by the program btoa.  The btoa/atob encoding
  18. ; has a 25% expansion rate (as opposed to 33% by uudecode).  Simple error
  19. ; checking is performed at the end to confirm that the entire file was
  20. ; decoded correctly.  No attempt to localize the error is made.  Btoa/atob
  21. ; is suitable only for ASCII transmission and hence is not appropriate for
  22. ; USENET transmission of binaries.
  23. ;
  24. ; I use btoa/atob to transfer binary files from a UNIX host to my IBM PC
  25. ; over a seven-bit channel, using the command line
  26. ;
  27. ;           btoa < desired.fil | kermit -s -
  28. ;
  29. ; receiving the file in MS-Kermit, then decoding it on my IBM PC.  This is
  30. ; faster than sending the binary directly since kermit uses eighth-bit quoting
  31. ; which essentially means that it takes 50% longer to transmit binaries
  32. ; as compared to ASCII files.
  33. ;
  34. ; I have used this program to decode dozens of binaries (including some
  35. ; ridiculously huge ones) and they all decoded fine, so I'm pretty certain
  36. ; that the bugs have been stomped out.  If you find one, contact me at the
  37. ; email address below.
  38. ;
  39. ; SPEED:
  40. ;
  41. ; rjb = Ray Berry's version of atob (1/21/89)
  42. ; v10 = Version 1.0 of my atob
  43. ; v11 = Version 1.1 of my atob
  44. ;
  45. ; Time to decode Ralf Brown's interrupt list (inter490.zoo) on a PC-XT:
  46. ;
  47. ; rjb = 1583 seconds (26 min 23 sec)
  48. ; v10 =  113 seconds ( 1 min 53 sec) Speedup: 14x
  49. ; v11 =   70 seconds ( 1 min 10 sec) Speedup: 22x   [1.6x faster than v10]
  50. ;
  51. ;
  52. ; ENCODING:
  53. ;
  54. ; Four bytes from the input are viewed as a 32-bit integer and converted
  55. ; to base 85.  "!" represents zero, the double-quote represents 1, and
  56. ; so on up to "u" representing 84.  As a special case, the 32-bit number
  57. ; zero is represented by the single character "z".  The file is headed
  58. ; by the string "xbtoa Begin" and is followed by
  59. ;
  60. ;       xbtoa End N Clen Clen E Ceor S Csum R Crot
  61. ;
  62. ; where Clen is the length of the encoded file (first in decimal, then
  63. ; in hex) and Ceor, Csum and Crot are three checksums (encoded in hex).
  64. ; Csum is Clen plus the sum of the characters.  Ceor is the exclusive-or
  65. ; of all the characters, and Crot is a checksum computed via the formula
  66. ;
  67. ;    Crot = (Crot rotated left one bit) + next_character
  68. ;
  69. ; These are crude checksums (not as robust as, say, CRC).
  70. ;
  71. ; IMPLEMENTATION:
  72. ;
  73. ; The program is pretty much a straightforward implementation of the
  74. ; decoding algorithm in assembly.  Of course, it is rather heavily
  75. ; hand-optimized.  In particular, SI and DI are used as global register
  76. ; values and inline macros are used rather frequently.  The only room
  77. ; for improvement I can think of is replacing the multiply-by-85
  78. ; with a shift-and-add algorithm.  (Which might even be slower on
  79. ; the 80386, whose multiplication algorithm has been pretty well-
  80. ; optimized.)
  81. ;
  82. ; HOW TO MAKE IT:
  83. ;
  84. ;       If you have TLINK           If you have MS's LINK
  85. ;       -----------------           ---------------------
  86. ;       masm atob;                  masm atob;
  87. ;       tlink atob;                 link atob;
  88. ;
  89. ; DISCLAIMER/COPYRIGHT:
  90. ;
  91. ; As usual, the author claims no responsibility for the behavior of
  92. ; the program, although he is pretty sure that it works fine.
  93. ;
  94. ; The program remains Copyright 1990 by Raymond Chen, but I make
  95. ; no attempt to restrict distribution in any form.  Just don't try
  96. ; to pass it off as your own.  This copyright is of dubious legal
  97. ; significance since the string "Copyright" appears nowhere in the
  98. ; binary.  I don't care.  If you want to violate my copyright, there
  99. ; isn't too much I can do to stop you.
  100. ;
  101. ; AUTHORSHIP:
  102. ;
  103. ; The program was written by Raymond Chen (raymond@math.berkeley.edu)
  104. ; in January 1990 or thereabouts.
  105. ;
  106. ; And now... The code:
  107.  
  108.         name    atob
  109.  
  110. ;*************************************************************************
  111. ; General equates
  112. ;*************************************************************************
  113.  
  114. DOSvec  equ     21h     ; DOS interrupt service vector
  115.  
  116. stdin   equ     0       ; DOS file handle
  117.  
  118. print   equ     9       ; Print a string to the console
  119. creat   equ     3ch     ; Create a new file
  120. close   equ     3eh     ; Close a file
  121. read    equ     3fh     ; Read from a handle
  122. write   equ     40h     ; Write to a handle
  123. exit    equ     4ch     ; End the process
  124.  
  125. openflg equ     20h     ; read-only, deny write
  126.  
  127. cr      equ     0dh
  128. lf      equ     0ah
  129.  
  130. bufsiz  equ     10240 ; size of I/O buffers.
  131.                       ; five times it must be less than 64K.
  132.  
  133.                       ; 5 = 4 + 1.  1 = a copy of the incoming data.
  134.                       ;             4 = each "z" codes four output bytes.
  135.                       ; worst case expansion is when somebody btoa's a
  136.                       ; file consisting entirely of zeros.
  137.  
  138. ;*************************************************************************
  139. ; Global register assignments:
  140. ;
  141. ;  SI = where to get the next byte from the input buffer
  142. ;  DI = where to send next byte to the output buffer
  143. ;  BP = What byte number within a block of four? (bcount)
  144. ;*************************************************************************
  145.  
  146. ;*************************************************************************
  147. ; Segmentation nonsense.
  148. ;*************************************************************************
  149.  
  150. _TEXT   segment byte public 'CODE'
  151. _TEXT   ends
  152.  
  153. _DATA   segment word public 'DATA'
  154. _DATA   ends
  155.  
  156. _BSS    segment word public 'BSS'
  157. _BSS    ends
  158.  
  159. _BSSEND segment byte public 'STACK'
  160. _BSSEND ends
  161.  
  162. _STACK  segment stack 'STACK'
  163.         dw      64 dup (?)
  164. _STACK  ends
  165.  
  166. DGROUP  GROUP   _DATA, _BSS, _BSSEND
  167.  
  168. ;*************************************************************************
  169. ; Initialized Global Variables
  170. ;*************************************************************************
  171. _DATA   segment word public 'DATA'
  172.  
  173. ClenL   dw      0       ; Length of converted file
  174. ClenH   dw      0
  175.  
  176. Ceor    label   byte    ; Checksum via exclusive or
  177. CeorL   dw      0
  178. CeorH   dw      0
  179.  
  180. Csum    label   dword   ; Checksum via summation
  181. CsumL   dw      0       ;   Low word
  182. CsumH   dw      0       ;   High word
  183.  
  184. Crot    label   dword   ; Checksum via rotation
  185. CrotL   dw      0       ;   Low word
  186. CrotH   dw      0       ;   High word
  187.  
  188. _DATA   ends
  189.  
  190. ;*************************************************************************
  191. ; Uninitialized Global Variables
  192. ;*************************************************************************
  193. _BSS    segment word public 'BSS'
  194.  
  195. fdout   label   word
  196.         dw      1 dup (?)       ; File handle for output
  197.  
  198. numbuf  label   byte
  199.         db      10 dup (?)      ; Numbers go here for decoding
  200.  
  201. inbuf   label   byte
  202.         db      bufsiz dup (?)  ; file input buffer
  203.         db      1 dup (?)       ; the extra byte is for a sentinel
  204.  
  205. outbuf  label   byte
  206.         db      4*bufsiz dup (?); file output buffer
  207.  
  208. _BSS    ends
  209.  
  210. ;*************************************************************************
  211. ; Macros
  212. ;*************************************************************************
  213.  
  214. ;*************************************************************************
  215. ; makestr:  Create a string with the specified label.
  216. ;           The optional third argument receives the length of the string.
  217. ;*************************************************************************
  218. makestr macro   l, s, c
  219. _DATA   segment word public 'DATA'
  220. l       db      s
  221.         ifnb    <c>
  222. c       equ     $-l
  223.         endif
  224. _DATA   ends
  225.         endm
  226.  
  227. ;*************************************************************************
  228. ; die:  Print a message and terminate